<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Registro di classe</title>
<meta name="description" content="" />
<meta name="keywords" content="scuola, iglesias, didattica, insegnamento, docenti, scuola media" />
<link rel="stylesheet" href="andreas08.css" type="text/css" media="screen,projection" />
<link href="../../../css/themes/default.css" rel="stylesheet" type="text/css"/>
<link href="../../../css/themes/mac_os_x.css" rel="stylesheet" type="text/css"/>
<link href="../../../css/themes/alphacube.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="../../../css/skins/aqua/theme.css" type="text/css" />
<script type="text/javascript" src="../../../js/prototype_js.php"></script>
<script type="text/javascript" src="../../../js/scriptaculous.js"></script>
<script type="text/javascript" src="../../../js/page.js.php"></script>
<script type="text/javascript" src="../../../js/window.js.php"></script>
<script type="text/javascript" src="../../../js/window_effects.js"></script>
<script type="text/javascript" src="../../../js/calendar.js.php"></script>
<script type="text/javascript" src="../../../js/lang/calendar-it.js"></script>
<script type="text/javascript" src="../../../js/calendar-setup.js"></script>
<script type="text/javascript">
var myvar, myvar2, win;
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);
var stid = 0;

var tempX = 0;
var tempY = 0;

function makeRequest(getvar) {
	var url = "cambia_orari_classe.php";
	myvar = getvar;
	//val = eval("document.forms[0]."+getvar+".value");
	val = $F('_'+getvar);
	var req = new Ajax.Request(url,
	  {
	    	method:'post',
	    	parameters: {update: 1, campo: getvar, value: val},
	    	onSuccess: function(transport){
	      		var response = transport.responseText || "no response text";
	      		if(response == "ko"){
	      			alert("Impossibile completare l'operazione richiesta. Riprovare tra qualche secondo o segnalare l'errore al webmaster");
		     		return;
	     		}
	     		else{
	     			//document.getElementById(myvar).innerHTML = "<a href=\"javascript:void(null);\" onclick=\"change(this,'"+myvar+"')\">"+eval("document.forms[0]."+myvar+".value")+"</a>";
	            	document.location.reload();
	     		}
	    	},
	    	onFailure: function(){ _alert("Si e' verificato un errore...") }
	  });
}

function change(link,textname){
     //link.innerText e link.text sono stessa cosa, c'è differenza tra ie e firefox
	if(link.innerText){
		testovecchio = link.innerText;
	}
	else{
		testovecchio = link.text;
	}
	//riscrivo il contenuto del div
	$(textname).innerHTML="<input type='text' style='font-size: 10px; width: 35px' name='"+textname+"' id='_"+textname+"' value='"+testovecchio+"'> <input type='button' style='border: 1px solid; font-size: 10px' value='Registra' onclick=\"makeRequest(\'"+textname+"\')\">";
}

function changeSchedule(getvar, id_alunno) {
	var url = "cambia_orario_alunno.php";
	myvar2 = getvar;
	//var val2 = eval("document.forms[0]."+getvar+".value");
	val2 = $F('_'+getvar);
	var req = new Ajax.Request(url,
	  {
	    	method:'post',
	    	parameters: {update: 1, alunno: id_alunno, campo: getvar, value: val2},
	    	onSuccess: function(transport){
	      		var response = transport.responseText || "no response text";
	      		if(response == "ok"){
					$(myvar2).innerHTML = "<a href=\"javascript:void(null);\" onclick=\"change2(this,'"+myvar2+"')\">"+eval("document.forms[0]."+myvar2+".value")+"</a>";
	     		}
	     		else{
	     			var dati = response.split(";");
	     			$("ingresso"+dati[1]).innerHTML = "<a href=\"javascript:void(null);\" onclick=\"change2(this,'ingresso"+dati[1]+"')\">A</a>";
					$("uscita"+dati[1]).innerHTML = "<a href=\"javascript:void(null);\" onclick=\"change2(this,'uscita"+dati[1]+"')\">A</a>";
	     		}
	    	},
	    	onFailure: function(){ alert("Si e' verificato un errore...");}
	  });
}

function change2(link,textname, id_alunno){
	//alert("ID alunno in change2: "+id_alunno);
     //link.innerText e link.text sono stessa cosa, c'è differenza tra ie e firefox
	if(link.innerText){
		testovecchio = link.innerText;
	}
	else{
		testovecchio = link.text;
	}
	//riscrivo il contenuto del div
	$(textname).innerHTML="<input type='text' style='font-size: 10px; width: 35px' name='"+textname+"' id='_"+textname+"' value='"+testovecchio+"'> <input type='button' style='border: 1px solid; font-size: 10px' value='Registra' onclick=\"changeSchedule(\'"+textname+"\', "+id_alunno+")\">";
}

function giustifica(id_alunno){
	//var fin = window.open_centered("elenco_giustificazioni.php?id="+id_alunno, "giust", 400, 250, "");
	var fin = new Window({className: "mac_os_x",  width:400, zIndex: 100, resizable: true, title: "Giustifica", url: "elenco_giustificazioni.php?id="+id_alunno, showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
	fin.showCenter(true);
}

function firma(id){
	var op = new Window({className: "mac_os_x",  width:550, zIndex: 100, resizable: true, title: "Firma il registro", url: "reg_orario.php?id_reg="+id, showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
	op.showCenter(true);
}

function show_menu(e, _stid){
	if (IE) { // grab the x-y pos.s if browser is IE
        tempX = event.clientX + document.body.scrollLeft;
        tempY = event.clientY + document.body.scrollTop;
    } else {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
    }  
    // catch possible negative values in NS4
    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;}  
    $('menu').style.top = parseInt(tempY)+"px";
    //alert(hid.style.top);
    $('menu').style.left = parseInt(tempX)+"px";
    $('menu').style.display = "inline";
    stid = _stid;
    return false;
}

function absent(){
	var url = "cambia_orario_alunno.php";
	var req = new Ajax.Request(url,
	  {
	    	method:'post',
	    	parameters: {update: 1, alunno: stid, campo: "", value: "A"},
	    	onSuccess: function(transport){
	      		var response = transport.responseText || "no response text";
	      		if(response == "ok"){
					
	     		}
	     		else{
	     			var dati = response.split(";");
	     			$("ingresso"+dati[1]).innerHTML = "<a href=\"javascript:void(null);\" onclick=\"change2(this,'ingresso"+dati[1]+"')\">A</a>";
					$("uscita"+dati[1]).innerHTML = "<a href=\"javascript:void(null);\" onclick=\"change2(this,'uscita"+dati[1]+"')\">A</a>";
	     		}
	     		$('menu').hide();
	    	},
	    	onFailure: function(){ alert("Si e' verificato un errore..."); }
	  });
}

function present(){
	var url = "cambia_orario_alunno.php";
	var req = new Ajax.Request(url,
	  {
	    	method:'post',
	    	parameters: {update: 2, alunno: stid, campo: "", value: "reset"},
	    	onSuccess: function(transport){
	      		var response = transport.responseText || "no response text";
	      		var dati = response.split(";");
	      		if(dati[0] == "ok"){
	      			$("ingresso"+stid).innerHTML = "<a href=\"javascript:void(null);\" onclick=\"change2(this,'ingresso"+stid+"')\">"+dati[1]+"</a>";
					$("uscita"+stid).innerHTML = "<a href=\"javascript:void(null);\" onclick=\"change2(this,'uscita"+stid+"')\">"+dati[2]+"</a>";
	     		}
	     		
	     		$('menu').hide();
	    	},
	    	onFailure: function(){ alert("Si e' verificato un errore..."); }
	  });
}

//var cal;

function go_to(){
	var dt = $F('date_to');
	ar = dt.split("/");
	nw_dt = ar[2]+"-"+ar[1]+"-"+ar[0];
	document.location.href = "registro_classe.php?data="+nw_dt;
}

function nbutton(){
	//$('dt').innerHTML = "Vai al "+$F('date_to');
	//$('dt').setAttribute('onclick', 'go_to()');
	$('delete').show();
}

function reset(){
	$('dt').innerHTML = "Giorno...";
	$('delete').hide();
}

function add_note(){
	win = new Window({className: "mac_os_x", url: "new_note.php?stid="+stid,  width:400, height:200, zIndex: 100, resizable: true, title: "Note disciplinare", showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});	
	win.showCenter(true);
	$('menu').hide();
}

</script>
</head>
<body>
<div id="container" >
<div id="header">
<h1><?php print $_SESSION['__config__']['intestazione_scuola'] ?></h1>
<h2>registro elettronico</h2>
</div>
<?php include "navigation.php" ?>
<div id="content">
<form>
<?php 
setlocale(LC_TIME, "it_IT");
$giorno_str = utf8_encode(strftime("%A", strtotime(date("Y-m-d"))));

if($res_registro->num_rows < 1){
?>
<div style="text-align: center; margin-left; auto; margin-right: auto; margin-top: 30px; margin-bottom: 30px; font-size: 14px; font-weight: bold; ">In questo giorno non sono previste lezioni</div>
<div style="width: 100%; text-align: center; margin-left; auto; margin-right: auto; margin-top: 30px; margin-bottom: 30px; font-size: 11px; font-weight: bold; ">
	<a href="registro_classe.php?data=<?php print $data ?>" style="float:left; " >&lt;&lt; Giorno precedente</a>
	<a id="dt" href="#" style="float: center; text-align: center">Giorno...</a>&nbsp;&nbsp;
		<a href="#" onclick="go_to()" id="delete" style="font-size: 11px; display: none"> (Vai)</a>
		<input type="hidden" name="date_to" id="date_to" onchange="nbutton()" />
		<script type="text/javascript">
				cal = Calendar.setup({
                date		: new Date(),
				inputField	: "date_to",
				displayArea	: "dt",
				ifFormat	: "%d/%m/%Y",
				showsTime	: false,
				firstDay	: 1,
				timeFormat	: "24",
				button		: "dt",
				dateStatusFunc :    function (date) {
            	    return (date.getDay() == 0) ? true : false;
				}
			});
        </script>
	<span style="float: right">Giorno successivo &gt;&gt;</span>
</div>
<?php 
}
else{
?>
<table class="registro" style="width: 100%; " cellpadding="0" cellspacing="0">
<thead>
<tr>
	<td colspan="4" class="reg_header" style="text-align: center; font-weight: bold"><?php print $_SESSION['__classe__']->to_string()." - Registro del giorno $giorno_str ".date("d/m/Y") ?><a href="#" onclick="firma('<?php print $_SESSION['registro']['id_reg'] ?>')" style="float: right; padding-right: 15px">Firma il registro</a></td>
</tr>
<tr>
	<td colspan="2" class="reg_header" style="text-align: center">Orario d'ingresso: <span id="ingresso" style="font-weight: bold; background-color: #e8eaec"><a href="#" style="color: black" onclick="change(this, 'ingresso')"><?php print substr($_SESSION['registro']['ingresso'], 0, 5) ?></a></span></td>
	<td colspan="2" class="reg_header" style="text-align: center">Orario d'uscita: <span id="uscita" style="font-weight: bold; background-color: #e8eaec"><a href="#" style="color: black" onclick="change(this, 'uscita')"><?php print substr($_SESSION['registro']['uscita'], 0, 5) ?></a></span></td>
</tr>
<tr>
	<td class="reg_header" style="width: 35%; font-weight: bold; padding-left: 8px">Alunno</td>
	<td class="reg_header" style="width: 15%; text-align: center; font-weight: bold">Entrata</td>
	<td class="reg_header" style="width: 15%; text-align: center; font-weight: bold">Uscita</td>
	<td class="reg_header" style="width: 35%; text-align: center; font-weight: bold">Note</td>
</tr>
</thead>
<tbody>
<?php 
$idx = 0;
while($al = $res_alunni->fetch_assoc()){
	// estrazione di ora ingresso e uscita
	$sel_orario_alunno = "SELECT ingresso, uscita FROM reg_alunni WHERE id_alunno = ".$al['id_alunno']." AND id_registro = ".$_SESSION['registro']['id_reg'];
	//print $sel_orario_alunno."<br />";
	$res_orario_alunno = $db->execute($sel_orario_alunno);
	$orario_alunno = $res_orario_alunno->fetch_assoc();
	if($orario_alunno['ingresso'] == "00:00:00")
		$entrata = "A";
	else
		$entrata = substr($orario_alunno['ingresso'], 0, 5);
	if($orario_alunno['uscita'] == "00:00:00")
		$uscita = "A";
	else
		$uscita = substr($orario_alunno['uscita'], 0, 5);
	$background = "";
	//if($idx%2)
	//	$background = "background-color: #e8eaec";
	
		
	// ricerca di assenze non giustificate
	$sel_assenze_ingiustificate = "SELECT data FROM reg_classi, reg_alunni WHERE id_reg = id_registro AND id_alunno = ".$al['id_alunno']." AND giustificata = 0 AND data < NOW() AND reg_classi.id_anno = ".$_SESSION['__current_year__']->get_ID()." ORDER BY data ";
	$res_assenze_ingiustificate = $db->execute($sel_assenze_ingiustificate);
	
	// ricerca di note
	$sel_note = "SELECT COUNT(id_nota) FROM note_disciplinari WHERE alunno = ".$al['id_alunno'];
	$num_note = $db->executeCount($sel_note);
?>
<tr>
	<td class="<?php if($idx == 0) print("reg_firstrow"); else print("reg_row"); ?>" style="width: 35%; padding-left: 8px"><a href="#" onclick="show_menu(event, <?php print $al['id_alunno'] ?>)" style="font-weight: normal; color: inherit"><?php print $al['cognome']." ".$al['nome']?></a></td>
	<td class="<?php if($idx == 0) print("reg_firstrow"); else print("reg_row"); ?>" style="width: 15%; text-align: center"><span id="ingresso<?php print $al['id_alunno'] ?>"><a href="#" style="color: black" onclick="change2(this, 'ingresso<?php print $al['id_alunno'] ?>', <?php print $al['id_alunno'] ?>)"><?php print $entrata ?></a></span></td>
	<td class="<?php if($idx == 0) print("reg_firstrow"); else print("reg_row"); ?>" style="width: 15%; text-align: center;"><span id="uscita<?php print $al['id_alunno'] ?>"><a href="#" style="color: black" onclick="change2(this, 'uscita<?php print $al['id_alunno'] ?>', <?php print $al['id_alunno'] ?>)"><?php print $uscita ?></a></span></td>
	<td class="<?php if($idx == 0) print("reg_firstrow"); else print("reg_row"); ?> reg_lastcell" style="width: 35%; text-align: center">
		<span id="note<?php print $al['id_alunno'] ?>">
			<?php if($res_assenze_ingiustificate->num_rows  > 0) print("<a style='text-decoration: underline; color: black; font-weight: normal' href='#' onclick='javascript: giustifica(".$al['id_alunno'].")'>".$res_assenze_ingiustificate->num_rows." assenze ingiustificate</a>") ?>
		</span>
		<span id="disc<?php print $al['id_alunno'] ?>">
			<?php if($num_note > 0){ if($res_assenze_ingiustificate->num_rows  > 0){ ?><span>&nbsp;&nbsp;|&nbsp;&nbsp;</span><?php } ?><a style='text-decoration: underline; color: black; font-weight: normal' href="dettaglio_note.php?al=<?php print $al['id_alunno'] ?>"><?= $num_note ?> note disciplinari</a><?php } ?>
		</span>
	</td>
</tr>
<?php
	$idx++; 
}
?>
</tbody>
<tr>
	<td colspan="4" class="reg_header" style="text-align: right; font-weight: bold; margin-right: 30px">&nbsp;</td>
</tr>
<tr>
	<td colspan="4" class="reg_header">
		<a href="registro_classe.php?data=<?php print $data ?>" style="float:left; width: 48%" >&lt;&lt; Giorno precedente</a>

		<a id="dt" href="#" style="float: center; text-align: center">Giorno...</a>&nbsp;&nbsp;
		<a href="#" onclick="go_to()" id="delete" style="font-size: 11px; display: none"> (Vai)</a>
		<input type="hidden" name="date_to" id="date_to" onchange="nbutton()" />
		<script type="text/javascript">
				cal = Calendar.setup({
                date		: new Date(),
				inputField	: "date_to",
				displayArea	: "dt",
				ifFormat	: "%d/%m/%Y",
				showsTime	: false,
				firstDay	: 1,
				timeFormat	: "24",
				button		: "dt",
				dateStatusFunc :    function (date) {
            	    return (date.getDay() == 0) ? true : false;
				}
			});
        </script>

		<span style="float: right">Giorno successivo &gt;&gt;</span>
	</td>
</tr>
</table>
<?php 
}
?>
</form>
<p></p>

</div>
<!-- menu contestuale -->
    <div id="menu" style="position: absolute; width: 200px; height: 80px; display: none; border: 1px solid; background-color: rgb(245,245,245); padding-left: 10px">
    	<a style="font-weight: normal" href="#" onclick="absent()">Segna come assente</a><br />
    	<a style="font-weight: normal" href="#" onclick="present()">Segna come presente</a><br />
    	<a style="font-weight: normal; padding-bottom: 10px" href="#" onclick="add_note()">Nota disciplinare</a><br />
    	<a style="font-weight: normal" href="#" onclick="$('menu').hide()">Chiudi</a>
    </div>
<!-- fine menu contestuale -->

<?php include "../footer.php" ?>

</div>
</body>
</html>